<template>
  <el-dialog v-model="drawer" :append-to-body="true" width="600px" @close="close" title="证书/证件详情">
    <el-form :model="form" size="small" ref="ruleRefForm" class="refForm" label-width="100px">
      <!--        <div v-for="(item, index) in form.list" :key="item.id">-->
      <el-row>
        <!-- :prop="`list.${index}.name`" -->
        <el-col :span="12">
          <el-form-item label="证书名称" prop="name" :rules="{required: true, message: '请输入证书名称', trigger: 'blur'}">
            <el-input v-model="form.name" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="证书/证件编号" prop="certNo">
            <el-input v-model="form.certNo" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="发证机构" prop="organize">
            <el-input v-model="form.organize" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="发证日期" prop="gainDate">
            <el-date-picker style="width:160px" v-model="form.gainDate" type="date" placeholder="请选择" size="small"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="证书级别" prop="grade">
            <el-input v-model="form.grade" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="有效期起始日期" prop="validStartDate">
            <el-date-picker style="width:160px" v-model="form.validStartDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择" size="small"/>
          </el-form-item>
          <el-form-item label="有效期到期日" prop="validEndDate">
            <el-date-picker style="width:160px" v-model="form.validEndDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择" size="small"/>
          </el-form-item>
          <el-form-item label="证件备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!--            <el-divider v-if="index < form.list.length - 1" style="margin: 0 0 18px" content-position="right">-->
      <!--                <el-button size="small" plain type="primary" link @click="delForm(index)">删除</el-button>-->
      <!--            </el-divider>-->
      <!--        </div>-->
    </el-form>
    <div class="footer-fixed">
      <!--        <el-row align="middle" class="add-fixed" @click="addCertificate">-->
      <!--            <el-icon color="var(&#45;&#45;el-color-primary-dark-2)"><ele-Plus /></el-icon>-->
      <!--            <span class="add-tips">新增证书/证件</span>-->
      <!--        </el-row>-->
      <el-row justify="center">
        <el-button type="primary" size="small" @click="submit(ruleRefForm)">保存</el-button>
        <el-button size="small" @click="resetForm(ruleRefForm)">取消</el-button>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {toRefs, computed, reactive, ref} from 'vue'
import {ElMessage} from 'element-plus'
import {createCertificate, editCertificate} from '/@/api/oa/staff/certificate'

export default {
  name: 'CertificateDetail',
  props: {
    dialogDrawer: {
      type: Boolean,
      default: false
    },
    staffCertificate: {
      type: Object,
      default: {}
    }
  },
  setup(props, context) {
    const {dialogDrawer} = toRefs(props)
    const drawer = computed({
      get: () => dialogDrawer.value,
      set: val => {
        context.emit('update:dialogDrawer', val)
      }
    })
    const {staffCertificate} = toRefs(props)
    const formData = computed({
      get: () => staffCertificate.value,
      set: val => {
        context.emit('update:staffCertificate', val)
      }
    })
    const close = () => {
      context.emit('update:dialogDrawer', false)
    }
    const state = reactive({
      form: formData
    })
    const ruleRefForm = ref()
    const submit = async (formEl) => {
      if (!formEl) return
      await formEl.validate(async(valid) => {
        if (valid) {
          try {
            if (state.form.id) {
              await editCertificate(state.form)
            } else {
              await createCertificate(state.form)
            }
            ElMessage.success('保存成功！')
            context.emit('update:dialogDrawer', false)
          } catch (e) {
            console.log(e)
          }
        }
      })
    }
    const resetForm = (formEl) => {
      if (!formEl) return
      context.emit('update:dialogDrawer', false)
      formEl.resetFields()
    }
    // const addCertificate = () => {
    //   state.form.list.push({})
    // }
    // const delForm = index => {
    //   state.form.list.splice(index, 1)
    // }
    return {
      drawer,
      formData,
      close,
      ruleRefForm,
      submit,
      resetForm,
      // addCertificate,
      // delForm,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped lang="scss">
:deep(.el-form-item) {
  align-items: flex-start;
}

.refForm {
  padding-bottom: 80px;
}

.footer-fixed {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  padding: 10px 0;
  background: #fff;
  z-index: 999;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;

  .add-fixed {
    padding-left: 20px;
    color: var(--el-color-primary);
    cursor: pointer;
    border-bottom: 1px dotted var(--el-border-color-light);
    padding-bottom: 10px;
    margin-bottom: 10px;

    .add-tips {
      margin-left: 5px;
      font-size: 13px;
    }
  }
}
</style>